package com.example.dish.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;

import com.example.dish.service.SetmealService;
import com.example.dish_model.dto.SetmealDTO;
import com.example.dish_model.entity.Setmeal;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author sjz
 * @since 2024-06-30
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Resource
    private SetmealService setmealService;


    /**
     * 分页查询套餐信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SetmealDTO>> getPage(long page, long pageSize, String name) {
        Page<SetmealDTO> setmealDTOPage = setmealService.getPage(page, pageSize, name);
        return Result.success(setmealDTOPage);
    }

    /**
     * 新增套餐,及其包含的菜品信息
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    public Result<String> add(@RequestBody SetmealDTO setmealDTO) {
        log.info("setmealDTO:{}", setmealDTO);
        setmealService.saveSetmealWithDish(setmealDTO);
        return Result.success("新增套餐成功");
    }

    /**
     * 修改套餐状态(停售启售接口)
     * 可以批量修改
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable("status") int status, @RequestParam List<String> ids) {
        log.info("status:{},ids:{}", status, ids);

        //根据传入的ids,查询数据库
        List<Setmeal> setmealList = setmealService.getByIds(ids);
        //修改套餐状态
        setmealService.updateStatus(status, setmealList);
        return Result.success("修改套餐状态成功");
    }


    /**
     * 反查询套餐信息接口
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealDTO> getById(@PathVariable("id") String id) {
        log.info("id:{}", id);
        SetmealDTO setmealDTO = setmealService.getSetmealWithDishById(id);
        return Result.success(setmealDTO);
    }

    /**
     * 根据条件查询套餐数据
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")
    public Result<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);

        return Result.success(list);
    }


    /**
     * 修改套餐基本信息,以及套餐对应的菜品信息
     *
     * @param setmealDTO
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐信息:{}", setmealDTO.toString());
        setmealService.updateSetmealWithDish(setmealDTO);
        return Result.success("修改套餐信息成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<String> ids) {
        log.info("ids:{}", ids);
        String result = setmealService.removeSetmealWihtDish(ids);
        return Result.success(result);
    }

}
